<?php
/**
 * MainMenu is a widget displaying main menu items.
 *
 * The menu items are displayed as an HTML list. One of the items
 * may be set as active, which could add an "active" CSS class to the rendered item.
 *
 */

class MainMenu extends CWidget
{
/*
	public function run()
	{
		$menu = $this->convertMenuTree(Part::model()->findByPK(1)->getNestedTree());
		
		$this->render('mainMenu', array(
			'menu'=>$menu
		));
	}
	
	private function convertMenuTree($tree, $tmp = array()){
		foreach($tree as $node){
			$c = count($tmp);
			$tmp[$c]['label'] = $node['node']->title;
			if(!empty($node['node']->url))
				$tmp[$c]['url'] = eval('return array('.$node['node']->url.');');
			
			if(isset($node['children']))
				$tmp[$c]['items'] = $this->convertMenuTree($node['children']);
		}
		
		return $tmp;
	}
*/
	public function run()
	{
		if(!$root = Part::model()->findByPK(1)) {
			$root = new Part;
			$root->id = 1;
			$root->lft = 1;
			$root->rgt = 2;
			$root->level = 0;
			$root->title = 'root';
			$root->alias = 'root';
			$root->save();
		}
		
		$controller=$this->controller;
		$action=$controller->action;
		
		// Поиск выбранного элемента
		$path = array();
		
		$flat = $root->getTree();
		$flag_strlen = 0;
		
		foreach($flat as $element){
			if($element->url) {
				$a = eval('return array('.$element->url.');');
				$url = trim($a[0],'/');
			} else {
				continue;
			}

			$route=$this->getController()->getRoute();

			$i = 0;
			do{
				$pos=strrpos($route, '/');

				if(strcmp($url,$route) != 0) {
					$route = substr($route, 0, $pos);
				} elseif($flag_strlen < strlen($route)) {
					$node = $element;
					$flag_strlen = strlen($route);
					$pos = false;
				}

				//echo $route.' '.$url;
				$i++;
				//echo '<br />';
			}while($pos && $i < 100);
			if($i == 99)
				echo 'error in MainMenu';
		}
		


		// Наполнение массивов
		$admin_root = Part::model()->find('alias=:alias', array(':alias'=>'admin'));
		if(!$admin_root){
			$admin_root = new Part;
			$admin_root->title = 'Админка';
			$admin_root->alias = 'admin';
			$admin_root->parent_id = 1;
			$admin_root->url = '\'admin\'';
			$root->appendChild($admin_root);
			
			$parts = new Part;
			$parts->title = 'Разделы';
			$parts->alias = 'parts';
			$parts->parent_id = $admin_root->id;
			$parts->url = '\'admin/parts\'';
			$admin_root->appendChild($parts);
		}

		$top = $admin_root->getChildNodes();
		if(isset($node))
			$path = array_splice($node->getPath(), 0, -2);
		else
			$path = array();

		$middle = array();
		$bottom = array();
		
		#foreach($path as $t) echo $t->title;
		
		foreach($top as $n=>$t)
			if(count($path)-1 >= 0 && $t->id == $path[count($path)-1]->id) {
				$top[$n]->_active = true;
				$middle[] = $t;
				continue;
			}
		
		$middle = array_reverse(array_merge(array_splice($path, 0, -1), $middle));

		if($node && $node->alias != 'admin') {
			$bottom = $node->getChildNodes();

			if($node->level == 2 || $node->hasChildNodes())
				$bottom = $node->getChildNodes();
			else {
				$bottom = $node->getParentNode()->getChildNodes();
				foreach($bottom as $n=>$t)
					if($t->id == $node->id) {
						unset($bottom[$n]);
					}
			}
		}
		
		$this->render('mainMenu',array(
			'top'=>$top,
			'middle'=>$middle,
			'bottom'=>$bottom
		));
	}
}
?>